package com.avic.rong.questionOrAnswer.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.avic.rong.BaseFragment;
import com.avic.rong.R;
import com.avic.rong.questionOrAnswer.activity.PersonalHomeActivity;
import com.avic.rong.questionOrAnswer.adapter.AchievementListAdapter;
import com.avic.rong.questionOrAnswer.api.GetSupplyListApi;
import com.avic.rong.questionOrAnswer.bean.GetSupplyBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class AchievementFragment extends BaseFragment {


    @BindView(R.id.recy_list)
    RecyclerView recyList;
    @BindView(R.id.loading_layout)
    LoadingLayout loading_layout;
    Unbinder unbinder;
    private List<GetSupplyBean> listData=new ArrayList<>();
    private  AchievementListAdapter achievementListAdapter;
    public  static  String PIC="logo";
    private String homeUserID;
    @Override
    protected void initView() {

        Bundle arguments = getArguments();
        homeUserID = arguments.getString("homeUserID");

        loading_layout.setStatus(LoadingLayout.SUCCESS);
        recyList.setLayoutManager(new LinearLayoutManager(getContext()));
        achievementListAdapter = new AchievementListAdapter(getContext(),listData);
        recyList.setAdapter(achievementListAdapter);
        initData();
    }
    private void initData() {
        if (!getSharedPreferencesUtil().getLogintoken().isEmpty()) {
            Map<String, String> map = new HashMap<>();
            map.put("token",getSharedPreferencesUtil().getLogintoken());
            if (TextUtils.isEmpty(homeUserID)){
                map.put("userId", "");
            }else {
                map.put("userId", homeUserID);
            }
            final GetSupplyListApi getSupplyListApi = new GetSupplyListApi((PersonalHomeActivity)getContext(), simpleOnNextListener);
            getSupplyListApi.setCache(false);
            getSupplyListApi.setMap(map);
            HttpManager.getInstance().doHttpDeal(getSupplyListApi);

            loading_layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
                @Override
                public void onReload(View v) {
                    HttpManager.getInstance().doHttpDeal(getSupplyListApi);
                    loading_layout.setStatus(LoadingLayout.LOADING);
                }

                @Override
                public void onReturn(View v) {
                    getActivity().onBackPressed();
                }
            });
            loading_layout.setStatus(LoadingLayout.LOADING);
        } else {
            loading_layout.setStatus(LoadingLayout.EMPTY);
        }
    }
    @Override
    protected void addListener() {

    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_achievement;
    }
    //融融号主页-成果列表
    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            listData.clear();
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)) {
                ToastTools.showToast(getContext(),subjects.getMsg());
                return;
            }
            loading_layout.setStatus(LoadingLayout.SUCCESS);
            List<GetSupplyBean> data = (List<GetSupplyBean>) subjects.getData();
            if (data!=null&&data.size()<=0){
                loading_layout.setStatus(LoadingLayout.EMPTY);
            }
            listData.addAll(data);
            achievementListAdapter.notifyDataSetChanged();
        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx",e.getMessage());
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                loading_layout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                loading_layout.setStatus(LoadingLayout.ERROR);
            }
        }
    };

}
